<template>
  <p>
    <a-space>
      <a-button type="primary" @click="handleQuery()">刷新</a-button>
    </a-space>
  </p>
  <!--表格-->
  <a-table :dataSource="tickets"
           :columns="columns"
           :pagination="pagination"
           @change="handleTableChange"
           :loading="loading">
    <template #bodyCell="{ column, record }">
      <template v-if="column.dataIndex === 'operation'">
      </template>
      <template v-else-if="column.dataIndex === 'station'">
        <strong>{{ record.startStation }}</strong><br>
        <strong>{{ record.endStation }}</strong>
      </template>
      <template v-else-if="column.dataIndex === 'time'">
        <div style="color: black;">{{ record.startTime }}</div>
        <div style="color: #666;">{{ record.endTime }}</div>
      </template>
      <template v-else-if="column.dataIndex === 'seatRow'">
        <span v-for="item in SEAT_COL_ARRAY" :key="item.code">
          <span v-if="item.code === record.seatCol &&  item.type === record.seatType">
            {{ record.seatRow + item.desc }}号
          </span>
        </span>
      </template>
      <template v-else-if="column.dataIndex === 'seatType'">
        <span v-for="item in SEAT_TYPE_ARRAY" :key="item.code">
          <span v-if="item.code === record.seatType">
            {{ item.desc }}
          </span>
        </span>
      </template>
    </template>
  </a-table>
</template>

<script setup>
import {onMounted, ref} from 'vue';
import request from "@/utils/request";
import {notification} from 'ant-design-vue'

// 车票枚举
const SEAT_COL_ARRAY = window.SEAT_COL_ARRAY;
// 车票枚举
const SEAT_TYPE_ARRAY = window.SEAT_TYPE_ARRAY;
// 新增按钮隐藏属性
const visible = ref(false);
// 表格加载
let loading = ref(false);
// 添加车票的属性
let ticket = ref({
  id: undefined,
  memberId: undefined,
  passengerId: undefined,
  passengerName: undefined,
  trainDate: undefined,
  trainCode: undefined,
  carriageIndex: undefined,
  seatRow: undefined,
  seatCol: undefined,
  startStation: undefined,
  startTime: undefined,
  endStation: undefined,
  endTime: undefined,
  seatType: undefined
});
// 表格数据
const tickets = ref([]);
// 分页的三个属性名是固定的
const pagination = ref({
  position: ['bottomCenter'],
  total: 0,
  current: 1,
  pageSize: 6,
  showSizeChanger: true,
  pageSizeOptions: ['6', '10', '20']
});
// 表格展示列
const columns = [
  {
    title: '乘客姓名',
    dataIndex: 'passengerName',
    key: 'passengerName',
  },
  {
    title: '日期',
    dataIndex: 'trainDate',
    key: 'trainDate',
  },
  {
    title: '车次编号',
    dataIndex: 'trainCode',
    key: 'trainCode',
  },
  {
    title: '车站',
    dataIndex: 'station',
  },
  {
    title: '时间',
    dataIndex: 'time',
  },
  {
    title: '箱序',
    dataIndex: 'carriageIndex',
    key: 'carriageIndex',
  },
  {
    title: '座位',
    dataIndex: 'seatRow',
    key: 'seatRow',
  },
  {
    title: '座位类型',
    dataIndex: 'seatType',
    key: 'seatType',
  },
];


// 请求 -- 查询车票
const handleQuery = (param) => {
  // 如果无参，则查询当前页
  if (!param) {
    param = {
      pageNum: pagination.value.current,
      pageSize: pagination.value.pageSize
    }
  }
  loading.value = true;
  request.get("/member/admin/ticket/list", {
    params: {
      pageNum: param.pageNum,
      pageSize: param.pageSize
    }
  }).then((result) => {
    loading.value = false;
    if (result.status) {
      tickets.value = result.content.records;
      // 设置分页控件的值
      pagination.value.current = param.pageNum;
      pagination.value.total = result.content.total;
    } else {
      notification.error({description: result.message});
    }
  });
};

// 表格换页，调用请求
const handleTableChange = (page) => {
  pagination.value = page
  handleQuery({
    pageNum: page.current,
    pageSize: page.pageSize,
  });
};

onMounted(() => {
  handleQuery({
    pageNum: 1,
    pageSize: pagination.value.pageSize
  });
});
</script>
